package com.ledim.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.ledim.activity.base.LedimBaseListActivity;
import com.ledim.adapter.TagsCardAdapter;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.FilterListBean;
import com.ledim.bean.FilterValBean;
import com.ledim.bean.TagChannelListDataResponse;
import com.ledim.ledimview.FilterTabsView;
import com.ledim.service.ApiService;
import com.ledim.service.LedimApiServiceFactory;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.ConstantUtil;
import com.ledim.utils.LogUtil;
import com.ledim.widget.view.TagFilterCallbackListener;
import com.letv.android.young.client.R;
import java.util.ArrayList;
import java.util.HashMap;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 乐次元标签筛选列表页
 */
public class LedimTagsActivity extends LedimBaseListActivity
        implements View.OnClickListener, TagFilterCallbackListener {

    private ImageView mImageBack;
    private TextView mTitle;
    private FilterTabsView mFilterTabsView;
    private ArrayList<FilterListBean> mFilters = new ArrayList<FilterListBean>();

    public static final String CARDID = "card_id";

    private HashMap<String, String> mKeys = new HashMap<String, String>();
    private FilterValBean orderFilter;
    private FilterValBean stFilter;
    //tabs数据
    String[] oneTabNames = { "最热", "最新" };//默认是最新
    String[] oneTabNamesIds = { "1", "2" };
    String[] twoTabNames = { "全部", "电视剧", "电影", "综艺", "动漫", "娱乐", "音乐", "自制", "乐次元" };//默认是全部
    String[] twoTabNamesIds = { "0", "2", "1", "11", "5", "3", "9", "1008", "47" };
    String[] keys = { "order", "st" };
    String[] names = { "排序", "属性" };
    //选择的条件
    private int order = 1;
    private int st = 0;
    private FrameLayout fl_filter_view;
    private TextView tv_filter_view;
    private String mediaId;
    private rx.Observer mObsevers;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_tags_channel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mediaId = getIntent().getStringExtra(ConstantUtil.ID);
        super.onCreate(savedInstanceState);
        initView();

    }

    @Override
    protected boolean enablePullRefresh() {
        return false;
    }

    @Override
    protected LedimBaseListAdapter getAdapter() {
        return new TagsCardAdapter(this, null);
    }

    @Override
    protected void getDatas() {
        if (mObsevers == null) {
            mObsevers = new LedimHttpObserver<TagChannelListDataResponse>() {

                @Override
                public void onCompleted() {
                    super.onCompleted();

                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                    setServerDataError();
                    LogUtil.e(e.getMessage());

                }

                @Override
                public void onNext(TagChannelListDataResponse labelListDataResponse) {
                    super.onNext(labelListDataResponse);
                    if (labelListDataResponse != null && labelListDataResponse.success) {
                        initTotalPages(labelListDataResponse.paged.total);
                        if (labelListDataResponse.ext != null &&
                                !TextUtils.isEmpty(labelListDataResponse.ext.name)) {
                            mTitle.setText(labelListDataResponse.ext.name);
                        } else {
                            mTitle.setText("标签");
                        }
                        setDatas(labelListDataResponse.data, labelListDataResponse.paged.more);
                    } else {
                        setServerDataError();
                    }
                }
            };
        }

        LedimApiServiceFactory.createService(ApiService.class)
                              .getLabelFiltersList(mediaId, mLoadPage + 1, getPageSize(), order, st)
                              .observeOn(AndroidSchedulers.mainThread())
                              .subscribeOn(Schedulers.io()).subscribe(mObsevers);
    }

    private void initView() {
        mImageBack = (ImageView) this.findViewById(R.id.iv_back);
        mImageBack.setVisibility(View.VISIBLE);
        mImageBack.setOnClickListener(this);

        mTitle = (TextView) this.findViewById(R.id.tv_title);
        //tabs
        mFilterTabsView = (FilterTabsView) View.inflate(this, R.layout.channels_tabs_layout, null);
        //筛选的条件
        fl_filter_view = (FrameLayout) findViewById(R.id.fl_filter_view);
        tv_filter_view = (TextView) findViewById(R.id.tv_filter_view);
        tv_filter_view.setText(getFilterText());

        ((ListView) mAdapterView).addHeaderView(mFilterTabsView);
        mFilters.addAll(getFilterData());
        mFilterTabsView.bindData(mFilters, this);
        mAdapter.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                                 int totalItemCount) {
                if (firstVisibleItem >= 1) {
                    fl_filter_view.setVisibility(View.VISIBLE);
                } else {
                    fl_filter_view.setVisibility(View.GONE);
                }

            }
        });

    }

    /**
     * 创建本地模拟数据
     * @return
     */
    private ArrayList<FilterListBean> getFilterData() {
        ArrayList<FilterListBean> datasList = new ArrayList<FilterListBean>();

        ArrayList<FilterValBean> oneValueFilters = new ArrayList<FilterValBean>();
        ArrayList<FilterValBean> twoValueFilters = new ArrayList<FilterValBean>();

        for (int j = 0; j < oneTabNames.length; j++) {
            oneValueFilters.add(new FilterValBean(oneTabNames[j], oneTabNamesIds[j]));
        }
        for (int j = 0; j < twoTabNames.length; j++) {
            twoValueFilters.add(new FilterValBean(twoTabNames[j], twoTabNamesIds[j]));
        }

        datasList.add(new FilterListBean(names[0], keys[0], oneValueFilters));
        datasList.add(new FilterListBean(names[1], keys[1], twoValueFilters));

        //配置默认的order和st
        mKeys.put("order", "1");
        mKeys.put("st", "0");

        return datasList;

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                //退出
                finish();
                break;

        }
    }

    @Override
    public void onFilter(FilterListBean filterBean, FilterValBean valueBean) {
        if (filterBean.key.equals("order")) {
            order = Integer.valueOf(valueBean.id);
            orderFilter = valueBean;
        } else if (filterBean.key.equals("st")) {
            st = Integer.valueOf(valueBean.id);
            stFilter = valueBean;
        }
        //刷新一下，重新获取数据
        pullRefresh();
        tv_filter_view.setText(getFilterText());
    }

    private String getFilterText() {
        StringBuilder filters = new StringBuilder();
        if (orderFilter == null) {
            filters.append("最热 • ");
        } else {
            filters.append(orderFilter.name).append(" • ");
        }
        if (stFilter != null) {
            filters.append(stFilter.name);
        }
        return filters.toString().substring(0, filters.lastIndexOf(" • "));
    }

}
